package leetcode;

import java.util.ArrayDeque;
import java.util.Deque;

/**
 * 【LeetCode题号: 27. 移除元素】
 * https://leetcode-cn.com/problems/remove-element/
 */
public class RemoveElements {
    public int removeElement(int[] nums, int val) {
        Deque<Integer> deque = new ArrayDeque<>();
        int size = 0;
        int count = 0;
        for (int i = 0; i < nums.length; i++) {
            if(nums[i] != val){
                deque.offer(nums[i]);
                count++ ;
            }else {
                size++;
            }
        }
        for (int i = 0; i < nums.length; i++) {
            if (!deque.isEmpty()){
                nums[i] = deque.pop();
            }else {
                nums[i] = 0;
            }
        }
        return nums.length - size;
    }
}
